package com.hkyp.ss.coursewebadmin.config;

import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.util.StringUtils;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.HashSet;
import java.util.Set;

@Configuration
@EnableSwagger2
@PropertySource(value = "classpath:swagger2.properties",encoding = "utf-8")
public class SwaggerConfig {

    @Value("${swagger.basePackage}")
    private String basePackage;
    @Value("${swagger.title}")
    private String title;
    @Bean
    public Docket createRestApi() {
        if(!StringUtils.isEmpty(basePackage)){
            basePackage = basePackage.trim();
        }
        Set<String> headers = new HashSet<>();
        headers.add("application/json; charset=UTF-8");
        return new Docket(DocumentationType.SWAGGER_2).produces(headers).consumes(headers).apiInfo(apiInfo())
                .securitySchemes(Lists.newArrayList(new ApiKey("token", "token", "header"))).select()
                .apis(RequestHandlerSelectors.basePackage(basePackage))
                .paths(PathSelectors.any()).build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title(title)
                .description(title)
                .version("1.0.0").build();
    }
}
